/* render all code textareas */
var codeEditorList = Array();
$(".codetextarea").each(function(i, obj){
	/* get the editors language from the class name */
	var codeEditorLang = "shell"; /* shell is default */
	var classList = obj.className.split(/\s+/);
	for(var c=0;c<classList.length;c++){
		if(classList[c].indexOf("codelang_")>=0){
			codeEditorLang = classList[c].substring(9);
		}
	}

	/* initialize this code editor */
	codeEditorList[i] = CodeMirror.fromTextArea(obj,{
		lineNumbers: true,
		lineWrapping: false,
		readOnly: true,
		viewportMargin: Infinity,
		mode: codeEditorLang
	});
});

var lastResizeWidth = 0;
function resizeCodeEditor(){
	/* Android Chrome fires a lot of resize
		events as the height keeps changing when
		the user scrolls and the navbar appears.
		This is why we check whether the width
		actually changed or not */
	if(lastResizeWidth!=$(window).width()){
		var editorWidth = $("#contentbody").width()-20;
		for(var c=0;c<codeEditorList.length;c++){
			codeEditorList[c].setSize(editorWidth,"auto");
		}
		/* update last resize width */
		lastResizeWidth = $(window).width();
	}
}